<template>
	<div>
		<div>
			<el-form :inline="true" class="demo-form-inline">
				<el-form-item>
					<el-button style="width: 200px;" type="primary" @click="initInsert">创建新角色</el-button>
				</el-form-item>

			</el-form>
			<!-- 新增模态框 -->
			<el-dialog title="新增" :visible.sync="dialogFormVisible">
				<el-form :model="TRole">
					<el-form-item label="名称" :label-width="formLabelWidth">
						<el-input v-model="TRole.name" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="角色名称" :label-width="formLabelWidth">
						<el-input v-model="TRole.nameZh" autocomplete="off"></el-input>
					</el-form-item>
					<!-- <el-form-item label="角色权限" :label-width="formLabelWidth">
						<el-tree :data="data" show-checkbox  node-key="id" ref="insertTree"
							highlight-current :props="defaultProps">
						</el-tree>
					</el-form-item> -->
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="dialogFormVisible = false">取 消</el-button>
					<el-button type="primary" @click="insertTRole">确 定</el-button>
				</div>
			</el-dialog>

		</div>
		<div>
			<el-table :data="roleList" height="300">
				<el-table-column fixed prop="id" label="角色编号" width="280" align="center">
				</el-table-column>
				<el-table-column prop="name" label="名称" width="280" align="center">
				</el-table-column>
				<el-table-column prop="nameZh" label="角色名称" width="280" align="center">
				</el-table-column>
				<el-table-column width="250" align="center" label="操作">
					<template slot-scope="scope">
						<el-button size="medium" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
						<el-button size="medium" type="danger"
							@click="handleDelete(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 修改模态框 -->
			<el-dialog title="修改" :visible.sync="dialogFormVisible01">
				<el-form :model="TRole">
					<el-form-item label="角色编号" :label-width="formLabelWidth">
						<el-input disabled v-model="TRole.id" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="名称" :label-width="formLabelWidth">
						<el-input v-model="TRole.name" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="角色名称" :label-width="formLabelWidth">
						<el-input v-model="TRole.nameZh" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="角色权限" :label-width="formLabelWidth">
						<el-tree :data="data" show-checkbox  node-key="id" ref="updateTree"
							highlight-current :props="defaultProps" :default-checked-keys="defaultCheck">
						</el-tree>
					</el-form-item>

				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="dialogFormVisible01 = false">取 消</el-button>
					<el-button type="primary" @click="updateTRole">确 定</el-button>
				</div>
			</el-dialog>
		</div>
	</div>
</template>

<script>
	import {
		putRequest
	} from '@/utils/api';
	export default {
		data() {
			return {
				id:1,
				dialogFormVisible01: false,
				//新增属性
				dialogFormVisible: false,
				TRole: {
					id: null,
					name: "",
					nameZh:""
				},
				formLabelWidth: '120px',
				defaultCheck:[],
				roleList: [],
				admin: {
					name: ""
				},
				data: [],
				defaultProps: {
					children: 'children',
					label: 'name'
				},
				updateTreeObj:{
					rid:null,
					mids:[]
				}
			}
		},
		mounted() {
			var admin = sessionStorage.getItem("admin");
			this.admin = admin;
			this.queryAll();
			this.queryAllType()
			
		},
		methods: {
			tMenuTypeQuery(){
				this.$postRequest("/tMenu/tMenuTypeQuery").then(res => {
					this.data = res.data.tMenuTypeQuery;
				});
			},
			queryAllType() {
				this.$postRequest("/tMenu/queryTMenuByAdminId/" + this.id).then(res => {
					this.data = res.data.menuList;
				});
			},
			initInsert() {
				this.TRole = {};
				this.dialogFormVisible = true;
				this.tMenuTypeQuery();
			},
			updateTRole() {
				this.defaultCheck=[];
				this.$postRequest("/tRole/insertAndUpdateTRole", this.TRole).then(res => {
					//获取角色权限id authToRole
					this.updateTreeObj.rid=this.TRole.id;
					this.updateTreeObj.mids=this.$refs.updateTree.getCheckedKeys();
					// console.log(this.updateTreeObj.rid)
					// console.log(this.updateTreeObj.mids)
					this.$postRequest("/tRole/authToRole/", this.updateTreeObj).then(res => {
					});
					this.dialogFormVisible01 = false;
					this.queryAll();
				});
			},
			insertTRole() {
				this.$postRequest("/tRole/insertAndUpdateTRole", this.TRole).then(res => {
					this.dialogFormVisible = false;
					this.queryAll();
				});
				
			},
			queryAll() {
				this.$postRequest("/tRole/queryTRoleList").then(res => {
					this.roleList = res.data.roleList;
				})
			},
			handleEdit(index, row) {
				console.log(index, row);
				this.dialogFormVisible01 = true;
				this.TRole = JSON.parse(JSON.stringify(row));
				this.tMenuTypeQuery();
				
				this.$postRequest("/tRole/queryTRoleMenu/"+this.TRole.id).then(res => {
					this.defaultCheck = res.data.tRoleMenuList;
				});
			},
			        
			handleDelete(index, row) {
				console.log(row.id)
				this.$confirm('此操作将永久删除, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$deleteRequest("/tRole/deleteTRole/" + row.id).then(res => {
						this.queryAll();
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});

			},

		}
	}
</script>

<style>
</style>